<?php

namespace app\admin\controller;




use app\material\model\UserDownloadModel;
use app\models\CampusModel;
use app\models\ClassRoomModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GradesDetailsModel;
use app\models\StudentModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\TeacherModel;
use app\models\UserModel;
use app\models\FollowUpLogModel;

class Enrollment extends Common
{


	/**
	 * 教师管理模型.
	 *
	 * @var TeacherModel
	 */
	protected $teacherModel;

	/**
	 * 教室管理模型.
	 *
	 * @var ClassRoomModel
	 */
	protected $classRoomModel;

	/**
	 * 班级管理详情模型.
	 *
	 * @var GradesDetailsModel
	 */
	protected $gradesdetailModel;
	/**
	 * DictionaryModel 字典模型.
	 *
	 * @var DictionaryModel
	 */
	protected $dictionaryModel;


	/**
	 * StudentModel 学员模型.
	 *
	 * @var StudentModel
	 */
	protected $studentModel;


	/**
	 * CourseModel 课程模型.
	 *
	 * @var CourseModel
	 */
	protected $courseModel;


	/**
	 * 用户模型.
	 *
	 * @var UserModel
	 */
	protected $userModel;


	/**
	 * CampusModel 校区管理模型.
	 *
	 * @var CampusModel
	 */
	protected $campusModel;

	/**
	 * 学生分班排课管理模型.
	 *
	 * @var StudentSubjectInfosClassModel
	 */
	protected $studentsubjectinfosclassModel;

	public function lists()
	{

		$enrollment_report = [];
		$followUpLogModel = new FollowUpLogModel();
		$item = [];
		$item['name'] = '跟进';
		$item['value'] = $followUpLogModel->where(['type' => 2])->count();
		$enrollment_report[] = $item;


		$result = (new UserModel())->where(['wtype' => 5])->select();
		foreach ($result as $key => $value){
			$item = [];
			$item['name'] = $value['name'];
			$item['value'] = $followUpLogModel->where(['type' => 2])->where(['user_id' => $value['id']])->count();  //总跟进
			$enrollment_report[] = $item;
		}

		$where = [];
		$where['status'] = 1;
		//来源渠道分析
		$channel_result = $this->dictionaryModel->where(['type' => 2])->select();
		$enrollment_channel_report =[];
		foreach ($channel_result as $key => $value){
			$item = [];
			$item['name'] = $value['name'];
			$item['value'] = $this->studentModel->where($where)->where(['channel' => $value['id']])->count();
			$enrollment_channel_report[] = $item;
		}

		$item = [];
		$item['name'] = '转介绍';
		$item['value'] = $this->studentModel->where($where)->where(['channel' => -1])->count();
		$enrollment_channel_report[] = $item;

		//意向级别
		$enrollment_intentional_report = [];
		for($i =0 ; $i <= 5; $i ++){
			$item = [];
			$item['name'] = $i.'类';
			$item['value'] = $this->studentModel->where($where)->where([ 'intentional_grade' => $i])->count();
			$enrollment_intentional_report[] = $item;
		}


		$enrollment_intentional_report = json_encode($enrollment_intentional_report);
		$enrollment_channel_report = json_encode($enrollment_channel_report);
		$data_enrollment_report = json_encode($enrollment_report);



		/************上门管理-试听课管理-转介绍管理**************/
		$where = [];
		$campus_where = [];
		$class_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$where['campus_id'] = get_current_campus_id();
			$campus_where['id'] = get_current_campus_id();
			$class_where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$where['campus_id'] = ['in',get_current_campus_id()];
			$campus_where['id'] = ['in',get_current_campus_id()];
			$class_where['campus_id'] =  ['in',get_current_campus_id()];
		}

		/***********班级********/
		$grades_class = $this->gradesdetailModel->where($where)->select();

		$where['status'] = 1;
		$campus_where['status'] = 1;
		$class_where['status'] = 1;
		$class_where['seat_set'] = ['neq',''] ;
		/*********老师*********/
		$teachers = $this->teacherModel->where($where)->select();
		/*******教室****/
		$rooms = $this->classRoomModel->where($class_where)->select()->toArray();


		if(get_current_type() == 5){  //员工级别
			$where['id'] = get_current_id();
		}
		$users = $this->userModel->where($where)->field('id,name')->select()->toArray();
		/*
		$arr[] = [
			'id' => 0,
			'name' => '公海',
		];
		$users = array_merge($users, $arr);
		*/
		$this->dictionary();
		$campus = $this->campusModel->where($campus_where)->select()->toArray();


		return $this->fetch('',[
			'enrollment_intentional_report' => $enrollment_intentional_report,
			'data_enrollment_report' => $data_enrollment_report,
			'enrollment_channel_report' => $enrollment_channel_report,
			'users' => $users,
			'campus' => $campus,
			'teachers' => $teachers,
			'grades_class' => $grades_class,
			'rooms' => $rooms,
		]);

	}

	//获取校区招生报表
	public function getenrollmentlist(){
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$result = $this->campusModel->getFollowStatisticsList($param);
			$this->success('success', '', $result, 0);
		}
	}

	//获取单个校区学生详情报表
	public function getstudentenrollmentlist(){

		if ($this->request->isAjax()) {
			$campus_id = input('campus_id');
			$param = $this->request->param();
			$param['campus_id'] = $campus_id;
			$result = $this->studentModel->getFollowStatisticsList($param);
			$this->success('success', '', $result, 0);
		}
	}


	//获取校区招生来源报表
	public function getstudentchannellist(){

		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$result = $this->dictionaryModel->getChannelStatisticsList($param);
			$this->success('success', '', $result, 0);
		}
	}


	//获取搜索时间段校区招生来源报表
	public function searchstudentchannellist(){
		if ($this->request->isAjax()) {
			$data =  input('post.');
			$where = [];
			$where_ = [];
			//时间段
			if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
				$where['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
				$where_['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
			}

			//来源渠道分析
			$channel_result = $this->dictionaryModel->where(['type' => 2])->select();
			$enrollment_channel_report =[];
			foreach ($channel_result as $key => $value){
				$item = [];
				$item['name'] = $value['name'];

				$where['status'] = 1;
				$where['channel'] = $value['id'];
				if(get_current_type() == 4 || get_current_type() == 5){
					$where['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){
					$where['campus_id'] = ['in',get_current_campus_id()];
				}
				$item['value'] = $this->studentModel->where($where)->count();

				$enrollment_channel_report[] = $item;
			}

			$item = [];
			$item['name'] = '转介绍';
			$where_['status'] = 1;
			$where_['channel'] = -1;
			if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
				$where_['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
				$where_['campus_id'] = ['in',get_current_campus_id()];
			}
			$item['value'] = $this->studentModel->where($where_)->count();
			$enrollment_channel_report[] = $item;

			$enrollment_channel_report = json_encode($enrollment_channel_report);
			$this->success('success', '', $enrollment_channel_report, 0);
		}
	}

	public function searchstudentmaturelist(){
		if ($this->request->isAjax()) {
			$data =  input('post.');
			$where = [];
			$where_ = [];
			//时间段
			if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
				$where['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
				$where_['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
			}

			//来源渠道分析
			$channel_result = $this->dictionaryModel->where(['type' => 2])->select();
			$enrollment_channel_report =[];
			foreach ($channel_result as $key => $value){
				$item = [];
				$item['name'] = $value['name'];

				$where['status'] = 1;
				$where['channel'] = $value['id'];
				if(get_current_type() == 4 || get_current_type() == 5){
					$where['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){
					$where['campus_id'] = ['in',get_current_campus_id()];
				}
				$item['value'] = $this->studentModel->where($where)->count();

				$enrollment_channel_report[] = $item;
			}

			$item = [];
			$item['name'] = '转介绍';
			$where_['status'] = 1;
			$where_['channel'] = -1;
			if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
				$where_['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
				$where_['campus_id'] = ['in',get_current_campus_id()];
			}
			$item['value'] = $this->studentModel->where($where_)->count();
			$enrollment_channel_report[] = $item;

			$enrollment_channel_report = json_encode($enrollment_channel_report);
			$this->success('success', '', $enrollment_channel_report, 0);
		}
	}


	//获取搜索时间段单个校区招生来源报表
	public function searchstudentonlychannellist(){
		if ($this->request->isAjax()) {
			$data =  input('post.');
			$campus_id = input('campus_id'); //校区ID
			$where = ['campus_id' => $campus_id];
			$where_ = ['campus_id' => $campus_id];

			if(get_current_type() == 5) {  //员工级别
				$where = ['user_id' => get_current_id()];
				$where_ = ['user_id' => get_current_id()];
			}

			//时间段
			if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
				$where['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
				$where_['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
			}

			//来源渠道分析
			$channel_result = $this->dictionaryModel->where(['type' => 2])->select();
			$enrollment_channel_report =[];
			foreach ($channel_result as $key => $value){
				$item = [];
				$item['name'] = $value['name'];
				if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
					$where['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
					$where['campus_id'] = ['in',get_current_campus_id()];
				}
				$where['status'] = 1;
				$where['channel'] = $value['id'];

				$item['value'] = $this->studentModel->where($where)->count();

				$enrollment_channel_report[] = $item;
			}

			$item = [];
			$item['name'] = '转介绍';
			$where_['status'] = 1;
			$where_['channel'] = -1;
			if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
				$where_['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
				$where_['campus_id'] = ['in',get_current_campus_id()];
			}
			$item['value'] = $this->studentModel->where($where_)->count();
			$enrollment_channel_report[] = $item;

			$enrollment_channel_report = json_encode($enrollment_channel_report);
			$this->success('success', '', $enrollment_channel_report, 0);
		}
	}


	//获取单个校区招生来源报表
	public function getstudentonlychannellist(){

		if ($this->request->isAjax()) {
			$campus_id = input('campus_id');
			$param = $this->request->param();
			$param['campus_id'] = $campus_id;
			$result = $this->dictionaryModel->getChannelOnlyStatisticsList($param);
			$this->success('success', '', $result, 0);
		}
	}



	//单个校区招生报表
	public function campus_report(){
		$campus_id = input('campus_id');
		$campus_name = $this->campusModel->get(['id' => $campus_id])['campus_name'];

		$enrollment_report = [];



		$item = [];
		$item['name'] = '线索';

		$where = [];
		$where['status'] = 1;
		$where['campus_id'] = $campus_id;
		if(get_current_type() == 5) {  //员工
			$where['user_id'] = get_current_id();
		}
		$item['value'] = $this->studentModel->where($where)->count();  //线索
		$enrollment_report[] = $item;


		$result = $this->dictionaryModel->where(['type' => 6])->select();

		$where = [];
		$where['status'] = 1;
		$where['campus_id'] = $campus_id;
		if(get_current_type() == 5) {  //员工
			$where['user_id'] = get_current_id();
		}
		foreach ($result as $key => $value){
			$item = [];
			$item['name'] = $value['name'];

			$where['follow_up'] = $value['id'];
			$item['value'] = $this->studentModel->where($where)->count();  //未分配
			$enrollment_report[] = $item;
		}

		//来源渠道分析
		$channel_result = $this->dictionaryModel->where(['type' => 2])->select();
		$enrollment_channel_report =[];

		$where = [];
		$where['status'] = 1;
		$where['campus_id'] = $campus_id;
		if(get_current_type() == 5) {  //员工
			$where['user_id'] = get_current_id();
		}
		foreach ($channel_result as $key => $value){
			$item = [];
			$item['name'] = $value['name'];
			$where['channel'] = $value['id'];
			$item['value'] = $this->studentModel->where($where)->count();
			$enrollment_channel_report[] = $item;
		}

		$item = [];
		$item['name'] = '转介绍';

		$where = [];
		$where['status'] = 1;
		$where['campus_id'] = $campus_id;
		$where['channel'] = -1;
		if(get_current_type() == 5) {  //员工
			$where['user_id'] = get_current_id();
		}
		$item['value'] = $this->studentModel->where($where)->count();  //转介绍
		$enrollment_channel_report[] = $item;

		//意向级别
		$enrollment_intentional_report = [];
		$where = [];
		$where['status'] = 1;
		$where['campus_id'] = $campus_id;
		if(get_current_type() == 5) {  //员工
			$where['user_id'] = get_current_id();
		}
		for($i =0 ; $i <= 5; $i ++){
			$item = [];
			$item['name'] = $i.'颗星';
			$where['intentional_grade'] = $i;
			$item['value'] = $this->studentModel->where($where)->count();
			$enrollment_intentional_report[] = $item;
		}


		$enrollment_intentional_report = json_encode($enrollment_intentional_report);

		$enrollment_channel_report = json_encode($enrollment_channel_report);
		$data_enrollment_report = json_encode($enrollment_report);

		return $this->fetch('',['enrollment_intentional_report' => $enrollment_intentional_report , 'data_enrollment_report' => $data_enrollment_report, 'enrollment_channel_report' => $enrollment_channel_report,  'campus_name' => $campus_name,  'campus_id' => $campus_id]);
	}


	//获取整个校区意向级别
	public function getstudentlevellist(){

		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$result = $this->studentModel->getLevelStatisticsList($param);
			$this->success('success', '', $result, 0);
		}
	}


	//搜索整个校区意向级别
	public function searchstudentlevellist(){

		if ($this->request->isAjax()) {
			$data =  input('post.');
			$where = [];
			//时间段
			if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
				$where['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
			}
			//意向级别
			$enrollment_intentional_report = [];
			for($i =0 ; $i <= 5; $i ++){
				$item = [];
				$item['name'] = $i.'类';
				$where['status'] = 1;
				$where['intentional_grade'] = $i;
				$item['value'] = $this->studentModel->where($where)->count();
				$enrollment_intentional_report[] = $item;
			}


			$enrollment_intentional_report = json_encode($enrollment_intentional_report);

			$this->success('success', '', $enrollment_intentional_report, 0);
		}
	}

	//获取单个校区意向级别
	public function getstudentonlylevellist(){

		if ($this->request->isAjax()) {
			$campus_id = input('campus_id');
			$param = $this->request->param();
			$param['campus_id'] = $campus_id;
			$result = $this->studentModel->getLevelOnlyStatisticsList($param);
			$this->success('success', '', $result, 0);
		}
	}


	//搜索单个校区意向级别
	public function searchstudentonlylevellist(){

		if ($this->request->isAjax()) {
			$data =  input('post.');
			$campus_id = input('campus_id');
			$where = ['campus_id' => $campus_id];
			if(get_current_type() == 5){  //员工级别
				$where['user_id'] = get_current_id();
			}

			//时间段
			if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
				$where['create_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
			}
			//意向级别
			$enrollment_intentional_report = [];
			for($i =0 ; $i <= 5; $i ++){
				$item = [];
				$item['name'] = $i.'颗星';
				$where['status'] = 1;
				$where['intentional_grade'] = $i;
				$item['value'] = $this->studentModel->where($where)->count();
				$enrollment_intentional_report[] = $item;
			}


			$enrollment_intentional_report = json_encode($enrollment_intentional_report);

			$this->success('success', '', $enrollment_intentional_report, 0);
		}
	}


	/**
	 *  实例化.
	 */
	protected function _initialize()
	{
		parent::_initialize();
		$this->studentsubjectinfosclassModel = new StudentSubjectInfosClassModel();
		$this->campusModel = new CampusModel();
		$this->courseModel = new CourseModel();
		$this->studentModel = new StudentModel();
		$this->dictionaryModel = new DictionaryModel();
		$this->userModel = new UserModel();
		$this->gradesdetailModel = new GradesDetailsModel();
		$this->classRoomModel = new ClassRoomModel();
		$this->teacherModel = new TeacherModel();
	}
}
